<?php

use App\Utils\Code;

/**
	 * 接口返回数据
	 * @param code 状态码
	 * @param msg 信息提示
	 * @param data 携带数据
	 * @return array 
	 */

	function renderData($data = [], $msg = 'SUCCESS', $code = 0)
	{
		return compact('code', 'msg', 'data');
	}

	/**
	 * 接口成功返回数据
	 * @param code 状态码
	 * @param msg 信息提示
	 * @param data 携带数据
	 * @return array 
	 */
	function renderSuccess($msg = 'SUCCESS', $code = 0, $data = [])
	{
		return compact('code', 'msg', 'data');
	}

	/**
	 * 接口失败返回数据
	 * @param code 状态码
	 * @param msg 信息提示
	 * @param data 携带数据
	 * @return array 
	 */
	function renderError($code = 1, $msg = 'ERROR', $data = [])
	{
		if (!$msg) $msg = Code::getMessage($code);
		return compact('code', 'msg', 'data');
	}

	/**
	 * 递归实现无限极分类
	 * @param $data 源数据
	 * @param $pid 父级id
	 * @return $list 
	 */
	function getTree($data, $pid = 0)
	{
		$list = [];
		foreach ($data as $key => $value) {
			if ($value['pid'] == $pid) {
				$value['spread'] = true;
				$value['children'] = getTree($data, $value['id']);
				$list[] = $value;
			}
		}
		return $list;
	}

	/**
	 * 递归生成无限极菜单
	 * @param $data 源数据
	 * @param $pid 父级ID
	 * @param $route 当前路由名称
	 * @return $list
	 */
	function createMenu($data, $route, $pid = 0)
	{
		$list = [];
		foreach ($data as $value) {
			if ($value['pid'] == $pid) {
				$value['is_open'] = $route == $value['route'] ? 1 : 0;
				$value['children'] = createMenu($data, $route, $value['id']);
				foreach ($value['children'] as $val) {
					if ($val['is_open'] == 1) $value['is_open'] = 1;
				}
				$list[] = $value;
			}
		}
		return $list;
	}